package com.izocen.rules.core.rule;

import java.util.Map;

import javax.script.ScriptEngine;


import com.izocen.rules.common.EasyStringUtils;

/**
 *@ClassName RuleFactor
 * @Description: 规则因子
 * 封装了一个表达式里面最低粒度的逻辑判断条件
 * @Author pengyongjun
 * @Date 2020年6月19日
 */
public class RuleUnit {
	
	/**
	 * 规则的唯一标识
	 */
	private String id;
	
	/**
	 * 规则分组标识
	 */
	private String groupId;
	
	/**
	 * 规则的脚本表达式
	 */
	private String express;

	/**
	 * 脚本引擎
	 */
	private ScriptEngine scriptEngine;
	
	public RuleUnit() {
		
	}
	
    public RuleUnit(String id,String groupId,String express,ScriptEngine scriptEngine) {
    	this.id = id;
    	this.groupId = groupId;
    	this.express = express;
    	this.scriptEngine = scriptEngine;
	}

    public boolean fire(Map<String,String> fact) {
    	String cmd = EasyStringUtils.processTemplate(express, fact);
    	try {
    		//System.out.println(cmd);
			boolean result = (Boolean)scriptEngine.eval(cmd);
			//System.out.println(result);
			return result;
		}catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }
    
    
    
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getExpress() {
		return express;
	}

	public void setExpress(String express) {
		this.express = express;
	}

	public ScriptEngine getScriptEngine() {
		return scriptEngine;
	}

	public void setScriptEngine(ScriptEngine scriptEngine) {
		this.scriptEngine = scriptEngine;
	}
	
	
    
	
	
	
	
	
}
